from symbide import __version__, __project_name__, __license__, \
                    __homepage__, __authors__, PATHS
import gtk, webbrowser
from os.path import splitext

def about():
    about = gtk.AboutDialog()
    about.set_name(__project_name__)
    about.set_version(__version__)
    about.set_license(__license__)
    def newTab(dialog, link):
        webbrowser.open_new_tab(__homepage__)
    gtk.about_dialog_set_url_hook(newTab)
    about.set_website(__homepage__)
    about.set_authors(__authors__)
    about.run()
    about.destroy()
    
def open():
    fileDialog = gtk.FileChooserDialog(\
                    title='Symbide - Open Worksheet',
                    action=gtk.FILE_CHOOSER_ACTION_OPEN,                                            
                    buttons=(   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    fileDialog.set_select_multiple(True)
    fileDialog.set_current_folder(PATHS['user'])
    fileDialog.add_shortcut_folder(PATHS['examples'])
    fileDialog.add_shortcut_folder(PATHS['user'])
    
    filter1 = gtk.FileFilter()
    filter1.add_pattern('*.symb')
    filter1.set_name('symb')
    fileDialog.add_filter(filter1)
    
    filter2 = gtk.FileFilter()
    filter2.add_pattern('*.py')
    filter2.set_name('py')
    fileDialog.add_filter(filter2)
    
    filter3 = gtk.FileFilter()
    filter3.add_pattern('*')
    filter3.set_name('All files')
    fileDialog.add_filter(filter3)
    
    response = fileDialog.run()
    if response == gtk.RESPONSE_OK:
        path = fileDialog.get_filenames()
    else:
        path = None
    fileDialog.destroy()
    return path

def select_path():
    fileDialog = gtk.FileChooserDialog(\
                    title='Symbide - Choose Path',
                    action=gtk.FILE_CHOOSER_ACTION_OPEN,                                            
                    buttons=(   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    fileDialog.set_current_folder(PATHS['user'])
    fileDialog.add_shortcut_folder(PATHS['examples'])
    fileDialog.add_shortcut_folder(PATHS['user'])
    
    response = fileDialog.run()
    if response == gtk.RESPONSE_OK:
        path = fileDialog.get_filename()
    else:
        path = None
    fileDialog.destroy()
    return path

def select_color():
    colorDialog = gtk.ColorSelectionDialog('Symbide - Select a color')
    colorDialog.colorsel.set_has_opacity_control(True)
    
    response = colorDialog.run()
    if response == gtk.RESPONSE_OK:
        color = colorDialog.colorsel.get_current_color()
    else:
        color = None
    colorDialog.destroy()
    return color

def saveAs(worksheet):
    fileDialog = gtk.FileChooserDialog(\
                    title='Symbide - Save Worksheet: '+worksheet.caption,
                    action=gtk.FILE_CHOOSER_ACTION_SAVE,                                            
                    buttons=(   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_SAVE, gtk.RESPONSE_OK))
    fileDialog.set_current_folder(PATHS['user'])
    fileDialog.add_shortcut_folder(PATHS['examples'])
    fileDialog.add_shortcut_folder(PATHS['user'])
    
    if worksheet.identifier == 'Worksheet':
        ext = 'symb'
    else:
        ext = 'py'
    
    filter1 = gtk.FileFilter()
    filter1.add_pattern('*.'+ext)
    filter1.set_name(ext)
    fileDialog.add_filter(filter1)
    
    filter2 = gtk.FileFilter()
    filter2.add_pattern('*')
    filter2.set_name('All files')
    fileDialog.add_filter(filter2)
    
    response = fileDialog.run()
    path = fileDialog.get_filename()
    fileDialog.destroy()
    if response != gtk.RESPONSE_OK:
        return False
    else:
        if not splitext(path)[1]:
            path += '.symb'
        return path        

def saveSingle(worksheet):
    text = 'Do you want to save changes in ' + worksheet.caption + '?'
    dialog = gtk.MessageDialog(
                      type=gtk.MESSAGE_QUESTION,
                      buttons=gtk.BUTTONS_YES_NO,
                      message_format=text)
    response = dialog.run()
    dialog.destroy()

    if response==gtk.RESPONSE_NO:
        return False
    elif response==gtk.RESPONSE_YES:
        return True
    return None

def saveMulti(worksheets):
    vbox = gtk.VBox()
    vbox.show()
    
    scroll = gtk.ScrolledWindow()
    scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
    scroll.show()
    scroll.add_with_viewport(vbox)
    
    header = gtk.Label('Which files do you want to save?')
    header.show()
    placeholder = gtk.Label('')
    placeholder.show()
    
    hbox = gtk.HBox()
    hbox.show()
    hbox.pack_end(placeholder)
    hbox.pack_end(header, expand=False)
    
    vbox1 = gtk.VBox()
    vbox1.show()
    vbox1.pack_end(scroll)
    vbox1.pack_end(hbox, expand=False)
    
    
    dialog = gtk.Dialog('Symbide - Save')
    dialog.add_button('close without saving', gtk.RESPONSE_CLOSE)
    dialog.add_button('cancel', gtk.RESPONSE_CANCEL)
    dialog.add_button('save', gtk.RESPONSE_YES)
    dialog.vbox.pack_end(vbox1)
    
    vbox.pack_start(gtk.Label())
    
    checkButton = {}
    for i in worksheets:
        checkButton[i] = gtk.CheckButton(i.caption)
        checkButton[i].set_active(True)
        checkButton[i].show()
        vbox.pack_start(checkButton[i], expand=False)
    
    response = dialog.run()
    dialog.destroy()
    
    if response==gtk.RESPONSE_CLOSE:
        return []
    elif response==gtk.RESPONSE_YES:
        buf = []
        for i in worksheets:
            if checkButton[i].get_active():
                buf.append(i)
        return buf
    return None

